/*********************************************************************
	Name: uart_bisskit.c         
	Description: Send data to BiSSkiT                 
			
	Author: Andrew Nicholas
	Date: 30/08/2011
*********************************************************************/
#include <uart.h>
#include "type.h"
#include "uart_bisskit.h" 

#define BAUDRATE 115200     //38400//921600//38400//2400//9600//
#define F_SYSCLK 1666667*16 // Fcy = ((20 MHz/3)/4)*PLL mulitplier

/*****************************************************************************
** Function name:		uart_init
**
** Descriptions:		Initalise UART to communicate with BiSSkiT.
**
** parameters:		    None
** Returned value:		None
**
*****************************************************************************/
void uart_init(){

  int ubrg;       /* Baud rate */
  int config1;    /* Operation settings */
  int config2;    /* TX & RX interrupt modes */ 

    config1 =   UART_EN &
                UART_IDLE_STOP &
                UART_DIS_WAKE &
                UART_DIS_LOOPBACK &
                UART_DIS_ABAUD &
                UART_NO_PAR_8BIT &
                UART_1STOPBIT;

    config2 =   UART_INT_TX &
                UART_TX_PIN_NORMAL &
                UART_TX_ENABLE &
                UART_INT_RX_CHAR &
                UART_ADR_DETECT_DIS ;

    ubrg    =   (((F_SYSCLK/BAUDRATE)/16));

    OpenUART2(config1, config2, ubrg);
}

/*****************************************************************************
** Function name:		uart_send
**
** Descriptions:		Send data from the packet buffer out the UART
**
** Parameters:		    None
** Returned value:		None
**
*****************************************************************************/
int uart_send(char buffer){
    // Wait until UART can send next data
    while(BusyUART2()){
		continue;
	}
	
    WriteUART2(buffer);
    return 1; // write successful
}

/*****************************************************************************
** Function name:		uart_close
**
** Descriptions:		Close UART connection
**
** Parameters:		    None
** Returned value:		None
**
*****************************************************************************/
void uart_close(){
    CloseUART2();
}
